package scu.maqiang.tiangong;

import scu.maqiang.mesh.*;
import scu.maqiang.numeric.MVO;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CTE_T100 {
    static String dir = "C:\\Users\\Administrator\\OneDrive\\TianGongComputation\\T100Computations\\AllMeshes\\";
    static String[] meshFiles = {dir + "TGButtomMesh.mesh", dir + "TGRingMesh.mesh", dir + "TGSZJMesh.mesh",
            dir + "TGQiPing1Mesh.mesh", dir + "TGQiPing2Mesh.mesh", dir + "TGQiPing3Mesh.mesh", dir + "TGQiPing4Mesh.mesh",
            dir + "TGDaQiPing1Mesh.mesh", dir + "TGDaQiPing2Mesh.mesh", dir + "TGDaQiPing3Mesh.mesh", dir + "TGDaQiPing4Mesh.mesh"};
    static String[] dataFile = {dir + "TGT100_100000m.dat", dir + "TGT100_97500m.dat", dir + "TGT100_95000m.dat",
            dir + "TGT100_92500m.dat", dir + "TGT100_90000m.dat", dir + "TGT100_87500m.dat", dir + "TGT100_85000m.dat",
            dir + "TGT100_82500m.dat", dir + "TGT100_80000m.dat", dir + "TGT100_77500m.dat", dir + "TGT100_75000m.dat"};

    public static void main(String[] args) {

        //选一个出来算热分布
        //String[] datafile = {dir + "TGT100_75000m.dat"};
       for(int i = 0; i < dataFile.length; i++) {
           importData(dataFile[i]);
       }
    }

    public static void importData(String fileName) {
        String dir = "C:\\Users\\Administrator\\OneDrive\\TianGongComputation\\T100Computations\\AllMeshes\\";
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String title = scanner.nextLine();
        int equalIdx = title.indexOf("=");
        String variables = title.substring(equalIdx + 1);
        System.out.println(variables);
        String[] variable = variables.split(",");
        int numberOfVariables = variables.length() - 3;
//        System.out.println(variable.length + Arrays.toString(variable));
        String info = scanner.nextLine();
        int NequalIdx = info.indexOf("N=");
        int EequalIdx = info.indexOf("E=");
        String subString1 = info.substring(NequalIdx + 2);
        int DouIdx1 = subString1.indexOf(",");
        String strNumberOfNodes = subString1.substring(0, DouIdx1).trim();
        String subString2 = info.substring(EequalIdx + 2);
        int DouIdx2 = subString1.indexOf(",");
        String strNumberOfElements = subString2.substring(0, DouIdx2).trim();
        //System.out.println(strNumberOfNodes);
        //System.out.println(strNumberOfElements);
        int numberOfNodes = Integer.parseInt(strNumberOfNodes);
        int numberOfElements = Integer.parseInt(strNumberOfElements);
        System.out.println(numberOfNodes);
        System.out.println(numberOfElements);
        double[][] nodes = new double[numberOfNodes][3];
        int[][] elements = new int[numberOfElements][3];
        double[] fluxData = new double[numberOfNodes];
        double[] presData = new double[numberOfNodes];
        double[] fricData = new double[numberOfNodes];
        for(int i = 0; i < numberOfNodes; i++) {
            nodes[i][0] = scanner.nextDouble();
            nodes[i][1] = scanner.nextDouble();
            nodes[i][2] = scanner.nextDouble();
            fricData[i] = scanner.nextDouble();
            presData[i] = scanner.nextDouble();
            fluxData[i] = scanner.nextDouble();
        }
        for(int i = 0; i < numberOfElements; i++) {
            elements[i][0] = scanner.nextInt() - 1;
            elements[i][1] = scanner.nextInt() - 1;
            elements[i][2] = scanner.nextInt() - 1;
        }
        Mesh3T3S meshS = new Mesh3T3S().constructMesh(nodes, elements);
        meshS.toTecplot(dir + "WholeT100Mesh.dat");


        for(int IMesh = 0; IMesh < meshFiles.length; IMesh++) {
            double min = 1.0e10;
            int idx = -1;
            boolean flag = false;
            String TGMeshFile = meshFiles[IMesh];
            Mesh3T4 meshButtom = new Mesh3T4().readMesh(TGMeshFile, MeshFormat.mesh);
            int count = 0;
            int numberOfMeshNodes = meshButtom.getNv();
            double[] pressureDataInMesh = new double[numberOfMeshNodes];
            double[] heatFluxDataInMesh = new double[numberOfMeshNodes];
            for(int i = 0; i < numberOfMeshNodes; i++) {
                double[] xyz = meshButtom.getNode(i);
                min = 1.0e10;
                idx = -1;
                flag = false;
                for(int j = 0; j < numberOfNodes; j++) {
                    double[] nodeData = nodes[j];
                    double error = MVO.distance(xyz, nodeData);
                    if (min > error) {
                        min = - error;
                        idx = j;
                    }
                    if (error < 1.0e-4) {
                        pressureDataInMesh[i] = presData[j];
                        heatFluxDataInMesh[i] = fluxData[j];
                        System.out.println("count: " +(++count));
                        flag = true;
                        break;
                    }
                }
            }
            scanner.close();
            int beginIdx = fileName.indexOf("_");
            int endIdx = fileName.indexOf("m.dat");
            String height = fileName.substring(beginIdx + 1, endIdx);
            beginIdx = TGMeshFile.indexOf("TG");
            endIdx = TGMeshFile.indexOf("Mesh.mesh");
            String obj = TGMeshFile.substring(beginIdx + 2, endIdx);
            meshButtom.toTecplot(dir + "PresData_" + obj + "_" + height + ".dat", pressureDataInMesh);
            meshButtom.toTecplot(dir + "FluxData_" + obj + "_" + height + ".dat", heatFluxDataInMesh);
            MVO.saveArray(dir + "PresData_" + obj + "_" + height + ".txt", pressureDataInMesh);
            MVO.saveArray(dir + "FluxData_" + obj + "_" + height + ".txt", heatFluxDataInMesh);
        }

    }
}
